package ${packageName}.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
<#if hasFuncList || hasFuncGetById || hasFuncExport || hasFuncImport>
import org.springframework.web.bind.annotation.GetMapping;
</#if>
<#if hasFuncInsert || hasFuncImport >
import org.springframework.web.bind.annotation.PostMapping;
</#if>
<#if hasFuncUpdate>
import org.springframework.web.bind.annotation.PutMapping;
</#if>
<#if hasFuncDelete>
import org.springframework.web.bind.annotation.DeleteMapping;
</#if>
<#if hasFuncGetById || hasFuncDelete>
import org.springframework.web.bind.annotation.PathVariable;
</#if>
<#if hasFuncInsert || hasFuncUpdate>
import org.springframework.web.bind.annotation.RequestBody;
	<#if hasFuncUnique>
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.ValidOperType;
		<#if moduleName == 'system'>
import com.lam.common.constant.${ModuleName}Constant;
		<#else>
import com.lam.${moduleName}.common.constant.${ModuleName}Constant;
		</#if>
	</#if>
</#if>
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lam.framework.operLog.annotation.Log;
<#if hasDictField>
import com.lam.common.components.dict.annotation.DictAutoTransfer;
</#if>
import com.lam.framework.annotation.ParamInject;
import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.framework.operLog.enums.BusinessType;
import ${packageName}.entity.${ClassName};
import ${packageName}.param.${ClassName}Param;
import ${packageName}.service.${ClassName}Service;
<#if hasFuncImport || hasFuncExport>
import com.lam.common.components.easyexcel.export.DownloadUtil;
</#if>
<#if hasFuncImport>
import ${packageName}.entity.imp.${ClassName}Imp;
import ${packageName}.listener.Upload${ClassName}Listener;
import java.util.Collections;
import java.util.Map;
import com.lam.common.components.easyexcel.imp.ImportUtil;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.write.handler.WriteHandler;
</#if>
<#if table.simpleTree>
import com.lam.common.base.domain.tree.TreeUtil;
import com.lam.common.base.page.TableData;
import com.lam.common.constant.CommonConstant;
	<#if hasFuncInsert || hasFuncUpdate || hasFuncDetail>
import com.lam.common.base.domain.tree.TreeNode;
import com.lam.common.utils.JsonUtil;
	</#if>
</#if>
<#if hasFuncInsert || hasFuncUpdate || hasFuncImport>
	<#if hasFuncInsert || hasFuncUpdate>
import org.springframework.validation.annotation.Validated;
		<#if hasFuncUnique && !uniqueFieldRequired>
import com.lam.common.utils.StringUtils;
		</#if>
	</#if>
	<#if hasFuncUpdate>
import com.lam.common.validate.group.Update;
	</#if>
	<#if hasFuncInsert || hasFuncImport>
import com.lam.common.validate.group.Add;
	</#if>
</#if>

/**
 * ${functionName}Controller
 * 
 * @author ${author}
 * @date ${datetime}
 */
@Log(title = "${functionName}")
@ParamInject
@RestController
@RequestMapping("/${'$'}{adminPath}/${codeModule.moduleShort}/${businessName}")
public class ${ClassName}Controller extends BaseController {

    @Autowired
    private ${ClassName}Service ${className}Service;

<#if hasFuncList>
    /**
     * 查询${functionName}列表
     */
	<#if table.simple>
    @Log(oper="分页查询")
    	<#if hasDictField>
    @DictAutoTransfer
    	</#if>
    <#elseif table.simpleTree>
    @Log(oper="列表查询")
    	<#if hasDictField>
    @DictAutoTransfer(deep = true)
    	</#if>
    </#if>
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:list')")
    @GetMapping("/list")
    <#if table.simple>
    public Result list(${ClassName}Param param) {
    
        startPage();
        List<${ClassName}> list = ${className}Service.selectList(param);
        return Result.ok(getTableData(list));
    }
    <#elseif table.simpleTree>
    public Result list(${ClassName}Param param) {
    
        List<${ClassName}> list = ${className}Service.selectList(param);
        list = TreeUtil.buildTree(list);
        return Result.ok(new TableData(list, list.size()));
    }
    </#if>
</#if>
<#if table.simpleTree && (hasFuncInsert || hasFuncUpdate || hasFuncDetail)>

    /**
     * ${functionName}下拉树列表
     */
    @Log(oper="${functionName}下拉树列表")
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:list')")
    @GetMapping("/treeselect")
    public Result treeselect(${ClassName}Param param) {
    
    	//PageHelper.orderBy("parent_id, sort_no");
        List<${ClassName}> list = ${className}Service.selectList(param);
        List<TreeNode> treeNodeList = JsonUtil.toJavaList(list, TreeNode.class, "id", "parentId", "name=>title", "sortNo");
        return Result.ok(TreeUtil.buildTree(treeNodeList, CommonConstant.TOP_PARENT_ID));
    }
</#if>
<#if hasFuncExport>

    /**
     * 导出${functionName}列表
     */
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:export')")
    @Log(businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result export(${ClassName}Param param) {
    
        withOrderBy();
        List<${ClassName}> list = ${className}Service.selectList(param);
        return Result.ok(DownloadUtil.exportExcel("${functionName}导出", list, ${ClassName}.class));
    }
</#if>
<#if hasFuncImport>

    @Log(businessType = BusinessType.EXPORT, oper = "下载导入模板")
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:import')")
    @GetMapping("/downloadTemplate")
    public Result downloadTemplate() {
        WriteHandler headRowCommentWriteHandler = ImportUtil.getHeadRowCommentWriteHandler(${ClassName}Imp.class, ${ClassName}.class, Add.class);
        <#if hasDictField>
        WriteHandler dropDownWriteHandler = ImportUtil.getDropDownWriteHandler(${ClassName}Imp.class, Add.class);
        </#if>
        return Result.ok(DownloadUtil.exportExcel("${functionName}导入模板", Collections.emptyList(), ${ClassName}Imp.class, 
            headRowCommentWriteHandler<#if hasDictField>, dropDownWriteHandler</#if>));
    }
    
    @Log(businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:import')")
    @PostMapping("/importData")
    public Result importData(MultipartFile file, boolean updateSupport) {
        
        logger.info("${functionName}数据导入, 开始接收文件数据...");
        Upload${ClassName}Listener readListener = new Upload${ClassName}Listener(${className}Service);
        readListener.setUpdateSupport(updateSupport);
        ImportUtil.importData("${functionName}", file, readListener);
        Map<String, Object> data = readListener.getOperResult();
        data.put("originalFilename", file.getOriginalFilename());
        return Result.ok(data);
    }
</#if>
<#if hasFuncGetById>

    /**
     * 获取${functionName}详细信息
     */
	<#if hasDictField>
    @DictAutoTransfer
    </#if>
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:query')")
    @GetMapping("/{${pkColumn.javaField}}")
    public Result getInfo(@PathVariable("${pkColumn.javaField}") String ${pkColumn.javaField}) {
    
        return Result.ok(${className}Service.selectById(${pkColumn.javaField}));
    }
</#if>
<#if hasFuncInsert>

    /**
     * 新增${functionName}
     */
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody ${ClassName} ${className}) {
    
    	<#if hasFuncUnique>
    		<#if !uniqueFieldRequired>
        if(StringUtils.isNotBlank(${className}.get${uniqueFieldFirstU}())) {
            ValidateUtil.validUnique(${className}, ValidOperType.ADD, ${ModuleName}Constant.${uniqueCodeConstant}, "${uniqueFieldName}已存在！");
        }
        	<#else>
        ValidateUtil.validUnique(${className}, ValidOperType.ADD, ${ModuleName}Constant.${uniqueCodeConstant}, "${uniqueFieldName}已存在！");
        	</#if>
        </#if>
        return toAjax(${className}Service.insert(${className}));
    }
</#if>
<#if hasFuncUpdate>

    /**
     * 修改${functionName}
     */
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody ${ClassName} ${className}) {
    
    	<#if hasFuncUnique>
    		<#if !uniqueFieldRequired>
    	if(StringUtils.isNotBlank(${className}.get${uniqueFieldFirstU}())) {
            ValidateUtil.validUnique(${className}, ValidOperType.UPDATE, ${ModuleName}Constant.${uniqueCodeConstant}, "${uniqueFieldName}已存在！");
        }
        	<#else>
        ValidateUtil.validUnique(${className}, ValidOperType.UPDATE, ${ModuleName}Constant.${uniqueCodeConstant}, "${uniqueFieldName}已存在！");
        	</#if>
        </#if>
        return toAjax(${className}Service.update(${className}));
    }
</#if>
<#if hasFuncDelete>

    /**
     * 删除${functionName}
     */
    @PreAuthorize("@ss.hasPm('${permissionPrefix}:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        return toAjax(${className}Service.deleteById(id));
    }
</#if>

}
